﻿using System;
using System.Web.UI;
using DevExpress.Web.ASPxMenu;
using System.Xml;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Common;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using SGRapidForm.Permission.Entity;
using System.Collections.Generic;

namespace SGDemo.Permission
{
    public partial class UserList : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");
        private IPermissionService permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        private List<ButtonEntity> _buttons = null;
        private List<ButtonEntity> Buttons 
        {
            get 
            {
                if (_buttons != null)
                {
                    return _buttons;
                }
                else 
                {
                    _buttons =  permissionService.User_GetButtonBy(ApplicationContext.Current.UserKeyid, this.GetType().BaseType.Name);
                    return _buttons;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            JSManager.RegAjax(Header, this.GetType().BaseType);

        }
        /// <summary>
        /// 添加快捷方式
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveFastAdd()
        {
            string userkeyid = ApplicationContext.Current.UserKeyid;
            string pagecode = this.GetType().Name;
            PageEntity page = _permissionService.Page_GetKeyidBypagename(pagecode);
            if (page != null)
            {
                return _permissionService.Shortcuts_Add(userkeyid, page.Keyid);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 自定义GridView回调重新绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {  
            gvwList.PageIndex = 0;
            gvwList.DataBind();
            gvwList.Selection.UnselectAll();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="keyids">Keyids集合</param>
        /// <returns>是否删除成功</returns>
        [AjaxPro.AjaxMethod]
        public bool BulkDelete(string keyids)
        {
           
            return permissionService.User_BulkDelete(keyids);
        }

        /// <summary>
        /// 批量审核通过
        /// </summary>
        /// <param name="keyids">Keyids集合</param>
        /// <returns>是否批量审核通过成功</returns>
        [AjaxPro.AjaxMethod]
        public bool AuditSuccess(string keyids)
        {
            return permissionService.User_BulkEdit("IsApproved=true&Username=xxxx", keyids);
        }

        /// <summary>
        /// 批量取消审核
        /// </summary>
        /// <param name="keyids">Keyids集合</param>
        /// <returns>是否批量取消审核成功</returns>
        [AjaxPro.AjaxMethod]
        public bool AuditFailure(string keyids)
        {
            return permissionService.User_BulkEdit("IsApproved=false", keyids);
        }

        //导出数据
        protected void btnExporter_Click(object sender, EventArgs e)
        {
            ASPxButton btn = sender as ASPxButton;
            string fileType = btn.CommandName;
            switch (fileType)
            {
                case "Xls": this.gvweList.WriteXlsToResponse(); break;
                case "Xlsx": this.gvweList.WriteXlsxToResponse(); break;
                case "Pdf": this.gvweList.WritePdfToResponse(); break;
                case "Csv": this.gvweList.WriteCsvToResponse(); break;
                case "Rtf": this.gvweList.WriteRtfToResponse(); break;
                default: break;
            }
        }

    }

    public class UserManage
    {
        private IPermissionService permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        public List<UserDetails> PagingGetUser(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            return permissionService.User_PagingGet(startRecord, maxRecords, sortColumns, filterConditions);
        }

        public int PagingGetUserCount(String filterConditions)
        {
            return permissionService.User_PagingGetCount(filterConditions);
        }
    }
}